<template>
    <!-- Content Wrapper. Contains page content -->
    <div class="content-wrapper">
      <!-- Content Header (Page header) -->
      <section class="content-header">
        <h1>
          系统管理
        </h1>

        <ol class="breadcrumb">
          <li><a href="#"> 控制台</a></li>
          <li><a href="#">基础数据管理</a></li>
          <li class="active" v-if="$route.params.id == 0">新增</li>
          <li class="active" v-if="$route.params.id > 0">编辑</li>
        </ol>
      </section>

      <!-- Main content -->
      <section class="content">
          <div class="row">
            <div class="col-xs-12">
                <div class="box box-solid">
                  <div class="box-header with-border">
                    <h3 v-if="$route.params.id == 0" class="box-title">基础数据新增</h3>
                    <h3 v-if="$route.params.id > 0" class="box-title">基础数据编辑</h3>
                  </div>
                  <!-- /.box-header -->
                  <!-- form start -->
                  <form class="form-horizontal">
                    <div class="box-body">
                      <div :class="['col-sm-6','form-group',{'has-error':errors.has('cate_id')}]">
                        <label class="col-sm-2 control-label">分类</label>
                          <div class="col-sm-10">
                            <select v-validate="'required|in:1,2'" class="form-control" v-model="cate_id" name="cate_id">
                              <option value="0">请选择</option>
                              <option value="1">客户类型</option>
                              <option value="2">服务类型</option>
                            </select>
                            <span class="help-block" v-if="errors.has('cate_id')">{{errors.first('cate_id')}}</span>
                          </div>
                      </div>
                      <div :class="['col-sm-6','form-group',{'has-error':errors.has('value')}]">
                        <label class="col-sm-2 control-label">内容</label>
                        <div class="col-sm-10">
                          <input v-validate="'required|max:15'" name="value" class="form-control" placeholder="15个字内" v-model="value">
                          <span class="help-block" v-if="errors.has('value')">{{errors.first('value')}}</span>
                        </div>
                      </div>
                    </div>
                    <!-- /.box-body -->
                    <div class="box-footer">
                      <button type="button" class="btn btn-primary" @click="submit">保存</button>
                    </div>
                    <!-- /.box-footer -->
                  </form>
                </div>
              <!-- /.box -->
            </div>
          </div>
      </section>
      <!-- /.content -->
    </div>
    <!-- /.content-wrapper -->
</template>

<script>

import BaseData from '@/modules/BaseData';

export default {

    mounted() {
        if(this.$route.params.id > 0){
            this.loadModel();
        }
    },

    data() {
        return {
            cate_id:0,
            value:''
        };
    },

    methods: {



        loadModel() {
            BaseData.show(this.$route.params.id)
            .then((response) => {
                this.cate_id = response.contents.cate_id;
                this.value = response.contents.value;
            });
        },

        submit() {
            this.$validator.validateAll().then((result) => {
                if(!result){
                    return;
                }
                var data = {
                    ...this.$data
                };

                var id = this.$route.params.id;

                if(id == 0){
                    BaseData.store(data)
                    .then((response) => {
                        $.message('操作成功');
                        this.$router.push({name:'BaseDataIndex'});
                    });
                }else{
                    BaseData.put(id,data)
                    .then((response) => {
                        $.message('操作成功');
                        this.$router.push({name:'BaseDataIndex'});
                    });
                }
            });
        }
    }

}
</script>
